package com.spring.sunflower.common;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.SignInBean;
import com.spring.sunflower.bean.TaskBean;
import com.spring.sunflower.bean.TaskItemBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.userinfo.AuthBeautyActivity;
import com.spring.sunflower.userinfo.AuthRealActivity;
import com.spring.sunflower.userinfo.PhotoWallActivity;
import com.spring.sunflower.userinfo.UserInfoEditActivity;
import com.spring.sunflower.userinfo.VideoShowListActivity;
import com.spring.sunflower.util.DisplayUtils;
import com.spring.sunflower.util.StatusBarUtil;
import com.spring.sunflower.widget.TaskItemView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/8/24<br/>
 * <p>
 * Describe:<br/>
 */
public class TaskActivity extends BaseActivity<TaskPresenter> implements CommonContract.TaskView, TaskItemView.OnTaskItemClickListener {
    private RoundedImageView rivAvatar;
    private TextView tvNickname;
    private TextView tvSignInfo, tvSign;
    private TaskItemView tivNewerTask, tivDailyTask;
    private Toolbar toolbar;
    private TextView tvTitle;
    private int mScrollY = 0;
    private NestedScrollView nestedScrollView;
    private ActionBar actionBar;
    private LinearLayout llSignInfo;
    private TextView tvSignAward, tvSignAwardType;
    private String gender = "1";

    @Override
    public void initView() {
        String avatar = getField(Constant.KEY_PHOTO, "");
        String nickname = getField(Constant.KEY_NICKNAME, "");
        //1是男2是女
        gender = getField(Constant.KEY_GENDER, "1");
        rivAvatar = findViewById(R.id.rivAvatar);
        Glide.with(this)
                .load(avatar)
                .placeholder(R.drawable.ic_placeholder)
                .into(rivAvatar);
        tvNickname = findViewById(R.id.tvNickname);
        tvNickname.setText(nickname);
        tvSignInfo = findViewById(R.id.tvSignInfo);
        if ("1".equals(gender)) {
            tvSignInfo.setText("未签到,签到可以领取金币奖励哦~");
        } else {
            tvSignInfo.setText("未签到,签到可以领取钻石奖励哦~");
        }
        tvSign = findViewById(R.id.tvSign);
        tivNewerTask = findViewById(R.id.tivNewerTask);
        tivDailyTask = findViewById(R.id.tivDailyTask);
        toolbar = findViewById(R.id.toolBar);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("任务奖励");
        llSignInfo = findViewById(R.id.llSignInfo);
        tvSignAward = findViewById(R.id.tvSignAward);
        tvSignAwardType = findViewById(R.id.tvSignAwardType);
        nestedScrollView = findViewById(R.id.nestedScrollView);
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            int lastScrollY = 0;
            int h = DisplayUtils.dip2px(TaskActivity.this, 170);
            int color = ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary) & 0x00ffffff;
            int titleColor = ContextCompat.getColor(getApplicationContext(), R.color.white) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
//                    buttonBar.setAlpha(1f * mScrollY / h);
                    toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
//                    ivParallax.setTranslationY(mOffset - mScrollY);
//                    tvTitle.setTextColor((255 * mScrollY / h) << 24 | titleColor);
                }
                lastScrollY = scrollY;
            }
        });
        toolbar.setBackgroundColor(0);
        tvTitle.setTextColor(getColorX(R.color.white));
        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }
        StatusBarUtil.immersive(this);
        StatusBarUtil.setPaddingSmart(this, toolbar);
        tivNewerTask.setOnTaskItemClickListener(this);
        tivDailyTask.setOnTaskItemClickListener(this);
        tvSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFastClick()) return;
                mPresenter.signIn();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getTask();
        mPresenter.querySignInRecord();
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_task;
    }

    @Override
    public TaskPresenter setPresenter() {
        return new TaskPresenter(this);
    }

    @Override
    public void showTaskList(TaskBean.DatasBean data) {
        List<TaskBean.DatasBean.TaskNewbieListBean> taskNewbieList = data.getTaskNewbieList();
        List<TaskBean.DatasBean.TaskDailyListBean> taskDailyList = data.getTaskDailyList();
        List<TaskItemBean> taskDailyData = new ArrayList<>();
        List<TaskItemBean> taskNewerData = new ArrayList<>();
        for (TaskBean.DatasBean.TaskNewbieListBean d : taskNewbieList) {
            TaskItemBean bean = new TaskItemBean();
            bean.setId(d.getId());
            bean.setTaskId(d.getTaskId());
            bean.setMemberId(d.getMemberId());
            bean.setAwardV(d.getAwardV());
            bean.setConditions(d.getConditions());
            bean.setCompleted(d.getCompleted());
            bean.setTaskCode(d.getTaskCode());
            bean.setTaskName(d.getTaskName());
            bean.setTaskDesc(d.getTaskDesc());
            bean.setTaskImgPath(d.getTaskImgPath());
            bean.setTaskType(d.getTaskType());
            bean.setUserType(d.getUserType());
            bean.setCreateTime(d.getCreateTime());
            bean.setStatus(d.getStatus());
            bean.setUpdateTime(d.getUpdateTime());
            taskNewerData.add(bean);
        }
        for (TaskBean.DatasBean.TaskDailyListBean d : taskDailyList) {
            TaskItemBean bean = new TaskItemBean();
            bean.setId(d.getId());
            bean.setTaskId(d.getTaskId());
            bean.setMemberId(d.getMemberId());
            bean.setAwardV(d.getAwardV());
            bean.setConditions(d.getConditions());
            bean.setCompleted(d.getCompleted());
            bean.setTaskCode(d.getTaskCode());
            bean.setTaskName(d.getTaskName());
            bean.setTaskDesc(d.getTaskDesc());
            bean.setTaskImgPath(d.getTaskImgPath());
            bean.setTaskType(d.getTaskType());
            bean.setUserType(d.getUserType());
            bean.setCreateTime(d.getCreateTime());
            bean.setStatus(d.getStatus());
            bean.setUpdateTime(d.getUpdateTime());
            taskDailyData.add(bean);
        }
        tivNewerTask.setTitle("新手任务");
        tivNewerTask.setData(taskNewerData);
        tivDailyTask.setTitle("日常任务");
        tivDailyTask.setData(taskDailyData);
    }

    @Override
    public void signSuccess(SignInBean.DatasBean data) {
        signed(data);
    }

    @Override
    public void noSign() {
        llSignInfo.setVisibility(View.GONE);
        tvSignInfo.setVisibility(View.VISIBLE);
    }

    @Override
    public void showSignRecord(SignInBean.DatasBean data) {
        signed(data);

    }

    public void signed(SignInBean.DatasBean data) {
        llSignInfo.setVisibility(View.VISIBLE);
        tvSignInfo.setVisibility(View.GONE);
        tvSignAward.setText(data.getAward());
        tvSign.setBackgroundResource(R.drawable.ic_btn_bg_gray_fill_corner_5);
        tvSign.setText("已签到");
        tvSign.setClickable(false);
        if ("1".equals(gender)) {
            tvSignAwardType.setText("金币");
        } else {
            tvSignAwardType.setText("钻石");
        }
    }

    @Override
    public void onTaskItemClick(String taskCode) {
        switch (taskCode) {
            case Constant.TASK_accost://搭讪一次
            case Constant.TASK_receiveGift://收到礼物
            case Constant.TASK_commentator://评论达人
                goHome(0);
                break;
            case Constant.TASK_bindMobile://绑定手机号
                break;
            case Constant.TASK_faceAuth://颜值认证
                startNewActivity(AuthBeautyActivity.class);
                break;
            case Constant.TASK_imChat://私信聊天
            case Constant.TASK_videoChat://视频聊天
                goHome(2);
                break;
            case Constant.TASK_firstCash://第一次提现
                break;
            case Constant.TASK_dailyFirstCash://每日提现
                break;
            case Constant.TASK_inviteRegister://邀请好友
                goHome(3);
                break;
            case Constant.TASK_realAuth://真人认证
                startNewActivity(AuthRealActivity.class);
                break;
            case Constant.TASK_releaseVoice://发布音遇
                break;
            case Constant.TASK_SupplementaryInfo://完善资料
                startNewActivity(UserInfoEditActivity.class);
                break;
            case Constant.TASK_uploadPhoto://上传照片
                startNewActivity(PhotoWallActivity.class);
                break;
            case Constant.TASK_uploadVideo://上传短视频
                startNewActivity(VideoShowListActivity.class);
                break;
        }
    }
}
